package hasthi.container;

import java.io.OutputStream;

import javax.xml.namespace.QName;

import org.apache.xmlbeans.XmlObject;
import org.xmlpull.v1.builder.XmlDocument;
import org.xmlpull.v1.builder.XmlElement;
import org.xmlpull.v1.builder.XmlInfosetBuilder;

import xsul.XmlConstants;
import xsul.soap.SoapUtil;
import xsul.soap11_util.Soap11Util;
import xsul.soap12_util.Soap12Util;
import xsul.ws_addressing.WsaMessageInformationHeaders;
import xsul.xbeans_util.XBeansUtil;

public class XSULMessageContext extends AbstractMessageContext{
    private XmlDocument envelope;
    private WsaMessageInformationHeaders wsah;
    private static XmlInfosetBuilder builder = XmlConstants.BUILDER;
    private SoapUtil soapFragrance;
    
    public XSULMessageContext(XmlDocument envelope,String toAddress){
        super(toAddress);
        processEnvelope(envelope);
    }

    public XSULMessageContext(XmlObject bodyContent,String toAddress, String soapAction){
        super(toAddress);
        soapFragrance = Soap12Util.getInstance();
        envelope = soapFragrance.wrapBodyContent(XBeansUtil.xmlObjectToXmlElement(bodyContent));
        this.soapAction = soapAction;
    }

    private void processEnvelope(XmlDocument envelope) {
        //System.out.println(builder.serializeToString(envelope));
        this.envelope = envelope;
        XmlElement documentElement = envelope.getDocumentElement();
        XmlElement soapHeaders = documentElement.element(null, XmlConstants.S_HEADER);
        if(soapHeaders  != null 
                && soapHeaders.element(null,WsaMessageInformationHeaders.ACTION_EL) != null) {
            this.wsah = new WsaMessageInformationHeaders(documentElement);
            this.soapAction = (wsah.getAction()!=null)?wsah.getAction().toString():"";
        }else{
            this.soapAction = "";  
            System.out.println(builder.serializeToString(envelope));
        }
        soapFragrance = SoapUtil.selectSoapFragrance(documentElement, new SoapUtil[] {
                Soap11Util.getInstance(), Soap12Util.getInstance() });
        XmlElement requestBody = soapFragrance.requiredBodyContent(documentElement);
        this.request = XBeansUtil.xmlElementToXmlObject(requestBody);
    }
    
    
    public WsaMessageInformationHeaders getAddressingHeaders(){
        return wsah;
    }

    public Object getHeader(QName name) {
        XmlElement soapHeaders = envelope.element(null, XmlConstants.S_HEADER);
        if(soapHeaders  != null){
            XmlElement header = soapHeaders.element(builder.newNamespace(name.getNamespaceURI()),name.getLocalPart());
            if(header.requiredTextContent()!= null){
                return header.requiredTextContent();
            }else{
                return XBeansUtil.xmlElementToXmlObject(header);
            }
        }
        return null;
    }

    public void writeTo(OutputStream outStream) {
        builder.serializeToOutputStream(envelope, outStream);
    }

    public XmlDocument getEnvelope() {
        return envelope;
    }

    public SoapUtil getSoapFragrance() {
        return soapFragrance;
    }
    
    
}
